package com.itheima.info.manager.dao;

import com.itheima.info.manager.domain.Student;

/*
库管类
 */
public class StudentDao implements BaseStudentDao{

    static {
        System.out.println("---------loading---------");
    }

    private static Student[] stus = new Student[5];

    //增添方法
    @Override
    public boolean addStudent(Student stu) {

        int index = -1;

        //遍历数组
        for (int i = 0; i < stus.length; i++) {
            if (stus[i] == null) {
                index = i;
                break;
            }
        }
        if (index != -1) {
            stus[index] = stu;
            return true;
        } else {
            return false;
        }
    }

    //查看方法
    @Override
    public Student[] findAllStudent() {
        return stus;
    }

    //修改方法
    @Override
    public void updateStudent(String updateId, Student stu) {
        int index = getIndex(updateId);
        stus[index] = stu;
    }

    //删除方法
    @Override
    public boolean deleteStudent(String delId) {

        int index = getIndex(delId);
        if (index == -1) {
            return false;
        } else {
            stus[index] = null;
            return true;
        }
    }

    //根据学号找到对应对象索引的方法
    @Override
    public int getIndex(String updateId) {
        //用标记为思想假设修改的学号在数组中不存在;
        int index = -1;
        for (int i = 0; i < stus.length; i++) {
            Student stu = new Student();
            stu = stus[i];
            if (stu != null && stu.getId().equals(updateId)) {
                index = i;
                break;
            }
        }
        return index;
    }
}
